package com.tian.job.station;
import java.time.LocalDateTime;

import com.tian.common.enums.StationGunCommunicateLog;
import com.tian.common.util.StationRedisPreKey;
import com.tian.entity.ChargeRecord;
import com.tian.mapper.ChargeRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import plus.ojbk.influxdb.core.InfluxdbTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * {@code @description:} 上传充电数据
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2025-03-29 19:27
 * {@code @version:} 1.0
 */
@Slf4j
public class MockChargingData  {

    private final String gunNo;
    private final ChargeRecordMapper chargeRecordMapper;
    private final InfluxdbTemplate influxDBTemplate;

    public MockChargingData(String gunNo, ChargeRecordMapper chargeRecordMapper, InfluxdbTemplate influxDBTemplate ) {
        this.gunNo = gunNo;
        this.chargeRecordMapper = chargeRecordMapper;
        this.influxDBTemplate = influxDBTemplate;
    }

    public void run() {
        log.info("开始：充电数据上传, gunNo:{}" , gunNo);
        try {
            ChargeRecord chargeRecord = chargeRecordMapper.selectByGunNo(gunNo);
            chargeRecord.setQuantity(chargeRecord.getQuantity().add(new BigDecimal("1")));

            StationGunCommunicateLog stationGunCommunicateLog = new StationGunCommunicateLog();
            stationGunCommunicateLog.setGunNo(gunNo);
            stationGunCommunicateLog.setLogType(0);
            stationGunCommunicateLog.setQuantity(new BigDecimal("1"));
            stationGunCommunicateLog.setParams("");
            stationGunCommunicateLog.setTime(LocalDateTime.now());

            chargeRecordMapper.updateByPrimaryKey(chargeRecord);

            // 保存到influxdb 这里只是演示，实际项目中可以用MQ来异步保存到influxdb
            influxDBTemplate.insert(stationGunCommunicateLog);
        } catch (Exception e) {
            log.error("充电数据上传失败" ,e);
        }
        log.info("结束：：充电数据上传, gunNo:{}" , gunNo);
    }
}
